# -*- coding: utf-8 -*-
# !/usr/bin/python
import base64
import gzip
import urllib

import MySQLdb
import StringIO
import urllib2

g_DatabaseIP = 'ginal.megowx.com'  # 'ginal.megowx.com'
dbport = 3306


# 数据压缩
def zipData(content):
    zbuf = StringIO.StringIO()
    zfile = gzip.GzipFile(mode='wb', compresslevel=9, fileobj=zbuf)
    zfile.write(content)
    zfile.close()
    return base64.b64encode(zbuf.getvalue())


# 打开数据库连接
try:
    conn = MySQLdb.connect(
        host=g_DatabaseIP,
        # port=dbport,
        user='ccr',
        passwd='Aitrip123',
        db='ccr',
        charset='utf8',
        local_infile=1
    )
except:
    print("Could not connect to MySQL server.")

sql = "INSERT INTO `fa_production_workorder` (`factory_id`, `processtype_id`, `machine_id`, `number`, `num_ok`, `num_ng`, " \
      "`chaidannum`, `chaidaninfo`, `content`, `subtitle`, `workordertype`, `specname`, `productname`, `fulllength`, `issuerecord`, " \
      "`info`, `stepnum`, `stepnames`, `ymmachine`, `ng1_num`, `ng2_num`, `ng3_num`, `ng4_num`, `ng5_num`, `ng6_num`, `ng7_num`, " \
      "`ng8_num`, `ng9_num`, `ng10_num`, `remark1`, `remark2`, `remark3`, `attachfiles`, `state`, `switch`, `weigh`) VALUES	" \
      "(1,74,0,'1567-20180223007',0,0,0,'','','受托工单','1567','1.15（+0.02/-0）*8.4(+0.2/-','1-D5-115-0084-C'," \
      "'38.25～38.35mm','2018-02-23 × 23,616','素材切断',4,'素材切断-刨光-生五清洗-刃部称重','',0,0,0,0,0,0,0,0,0,0,'','','','','0',0,0)," \
      "(1,74,0,'1567-20180223007',0,0,0,'','','受托工单','1567','1.15（+0.02/-0）*8.4(+0.2/-','1-D5-115-0084-C'," \
      "'38.25～38.35mm','2018-02-23 × 23,616','素材切断',4,'素材切断-刨光-生五清洗-刃部称重','',0,0,0,0,0,0,0,0,0,0,'','','','','0',0,0)"

str1 = zipData(sql)
print
str1

values = {"sql": str1}
data = urllib.urlencode(values)
url = "http://ginal.megowx.com:90/api/demo/doSQL"
request = urllib2.Request(url, data)
response = urllib2.urlopen(request)
print
response.read()
